from fastapi import APIRouter, Query
from pydantic import BaseModel
import os
from plugins.common import response_success, response_fail, get_first_line, now_date

router = APIRouter(prefix='/api')
class ApiResponse(BaseModel):
    message: str = Query('', description="消息内容")
    code: int = Query(10000, description="消息编号， 10000 表示成功， 其他为错误编号")
### 健康 ###
class health_info(BaseModel):
    api: str = Query('yes', description="api接口服务状态")
    redis: str = Query('yes', description="redis服务状态")
    rasa_core: str = Query('yes', description="rasa 核心服务状态")
    rasa_actions: str = Query('yes', description="actions 动作服务状态")
class health_response(ApiResponse):
    data:health_info = Query([], description="信息内容")

@router.get("/health", summary='健康检查接口',response_model=health_response, tags=['健康'])
def health():
    """
    检查系统内各个服务是否正常

    服务说明:
    - all: 所有服务是不是正常， yes表示都正常, no表示至少有一个服务不正常
    - api: api接口服务
    - redis: 缓存服务
    - rasa core: rasa 核心
    - rasa actions: rasa 动作服务

    返回： yes  表示服务正常  no 表示 服务有问题
    """

    data = {}

    # rasaCoreHealth = rasa_core_health()
    redisHealth = cache.health()
    data["api"] = 'yes'
    data["redis"] = redisHealth
    # data["rasa_core"] = rasaCoreHealth
    # data["rasa_actions"] = 'yes'
 
    
    return response_success(data)


### 知识库 ###
from tools.zsk.zsk import get_doc_list, get_doc_info, del_doc, save_doc, find_doc
class zishiku_params(BaseModel):
    typename: str = Query(default=..., description="类型: 停水公告/水价/自定义文本类型")
 
class zishiku_add_params(zishiku_params):
    title: str = Query(default=..., description="标题")
    content: str = Query(default=..., description="内容")

class zishiku_info_params(zishiku_params):
    doc_id: str = Query(default=..., description="文档编号")

class zishiku_search_params(zishiku_params):
    content: str = Query(default=..., description="搜索内容")

class zishiku_list(BaseModel):
    filename: str = Query(description="文件名")
    doc_id: str = Query(description="文档id")
    line_text: str = Query(description="第一行内容")
class zishiku_info(BaseModel):
    doc_id: str = Query(description="文档id")
    content: str = Query(description="内容")
class zishiku_search(BaseModel):
    title: str = Query(description="标题或者文件路径")
    content: str = Query(description="内容")
    score: int = Query(description="分值")

class zishiku_list_response(ApiResponse):
    data: list[zishiku_list] = Query([], description="知识库列表")
class zishiku_info_response(ApiResponse):
    data: zishiku_info = Query([], description="知识库详情")
class zishiku_action_response(ApiResponse):
    data: bool = Query(description="删除状态。 true 删除成功， false 删除失败")
class zishiku_search_response(ApiResponse):
    data: list[zishiku_search] = Query([], description="搜索到的内容信息")

@router.post("/zhishiku/list", summary="获得知识库内容列表", description="返回知识库内容列表", response_model=zishiku_list_response, tags=['知识库'])
def zhishik_list(params_dict: zishiku_params):
    params = params_dict.dict()
    typename = params.get('typename')
    userid = params.get('userid')
    if userid == '' :
        userid = None

    file_list = []
    for filepath, filename in get_doc_list(typename, userid):
        basename = os.path.splitext(os.path.basename(filename))[0]
        line_text = get_first_line(filepath+'/'+filename)
        file_list.append({"filename":filename, 'doc_id': basename, 'line_text':line_text})
    
    return response_success(file_list)

@router.post("/zhishiku/info", summary="获得知识库内容", description="返回知识库内容详情", response_model=zishiku_info_response, tags=['知识库'])
def zhishik_info(params_dict: zishiku_info_params):
    params = params_dict.dict()
    typename = params.get('typename')
    doc_id = params.get('doc_id')
    userid = params.get('userid')
    if userid == '' :
        userid = None

    fileinfo = get_doc_info(doc_id, typename, userid)
    return response_success(fileinfo)

@router.post("/zhishiku/del", summary="删除指定类型下的知识库内容", description="返回是否成功", response_model=zishiku_action_response, tags=['知识库'])
def delzishiku(params_dict: zishiku_info_params):
    params = params_dict.dict()
    typename = params.get('typename')
    doc_id = params.get('doc_id')
    userid = params.get('userid')
    if userid == '' :
        userid = None

    res = del_doc(doc_id, typename, userid)
    return response_success(res)

@router.post("/zhishiku/add", summary="添加指定类型下的知识库内容", description="返回是否成功",  response_model=zishiku_action_response, tags=['知识库'])
def addzhishiku(params_dict: zishiku_add_params):
    params = params_dict.dict()
    typename = params.get('typename')
    title = params.get('title')
    content = params.get('content')
    userid = params.get('userid')
    if userid == '' :
        userid = None

    res = save_doc(title, content, typename, userid)
    return response_success(res)
@router.post("/zhishiku/search", summary="搜索知识库内容", description="返回搜索到的知识库内容",  response_model=zishiku_search_response, tags=['知识库'])
def searchzhishiku(params_dict: zishiku_search_params):
    params = params_dict.dict()
    typename = params.get('typename')
    content = params.get('content')
    userid = params.get('userid')
    if userid == '' :
        userid = None

    res = find_doc(content, typename, userid)
    return response_success(res)

### 大语音模型功能 ###